package com.example.demo.controller;

import com.example.demo.pojo.Score;
import com.example.demo.service.IScoreService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/Score")
public class ScoreController {

    @Autowired
    private IScoreService ScoreService;

    @RequestMapping("/selectScore")
    public void selectScore(HttpServletResponse response) throws IOException {
        List<Score> list=ScoreService.selectScore();
        Gson gson = new Gson();
        String json = gson.toJson(list);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/selectScoreByOpenid")
    public void selectScoreByOpenid(HttpServletResponse response, String openid) throws IOException {
        List<Score> list=ScoreService.selectScoreByOpenid(openid);
        Gson gson = new Gson();
        String json = gson.toJson(list);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/selectScoreByOpenidLimit")
    public void selectScoreByOpenidLimit(HttpServletResponse response, String openid) throws IOException {
        Gson gson = new Gson();
        String json = gson.toJson(ScoreService.selectScoreByOpenidLimit(openid));
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/addScore")
    public void addScore(HttpServletResponse response,Score Score, String openid, Integer num, String ifc, String name, String dur, Integer tid, Integer rid) throws IOException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(df.format(new Date()));
        Score.setNum(num);
        Score.setIfc(ifc);
        Score.setName(name);
        Score.setOpenid(openid);
        Score.setTime(df.format(new Date()));
        Score.setDur(dur);
        Score.setTid(tid);
        Score.setRid(rid);
        ScoreService.insertSelective(Score);
        List<Score> list=ScoreService.selectScoreByOpenid(openid);
        Gson gson = new Gson();
        String json = gson.toJson(list);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }
}
